package com.kedacom.freedomLabel.model.bean;

import android.arch.persistence.room.Entity;
import android.arch.persistence.room.Ignore;
import android.arch.persistence.room.PrimaryKey;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.kedacom.freedomLabel.label.annotation.TemplateName;
import com.kedacom.freedomLabel.utils.Cn2Spell;

/**
 * 模板
 *
 * @author licheng
 * @since 2019/12/5 19:26
 */
@Entity
public class Template extends IFirstLetter implements Parcelable {

    @TemplateName
    @PrimaryKey
    @NonNull
    public String type = TemplateName.LABEL_0;

    public int width;
    public int height;

    public Position title;

    public Position id1;
    public Position id2;
    public Position id3;
    public Position id4;
    public Position id5;
    public Position id6;

    public Position companyName;
    public Position companyContactNumber;
    public Position companyFaxNumber;
    public Position companyAddress;

    public Position orderBatchNumber;
    public Position orderSerialNumber;
    public Position orderDealTime;

    public Position qr;

    public Template() {

    }

    protected Template(Parcel in) {
        type = in.readString();
        width = in.readInt();
        height = in.readInt();
        title = in.readParcelable(Position.class.getClassLoader());
        id1 = in.readParcelable(Position.class.getClassLoader());
        id2 = in.readParcelable(Position.class.getClassLoader());
        id3 = in.readParcelable(Position.class.getClassLoader());
        id4 = in.readParcelable(Position.class.getClassLoader());
        id5 = in.readParcelable(Position.class.getClassLoader());
        id6 = in.readParcelable(Position.class.getClassLoader());
        companyName = in.readParcelable(Position.class.getClassLoader());
        companyContactNumber = in.readParcelable(Position.class.getClassLoader());
        companyFaxNumber = in.readParcelable(Position.class.getClassLoader());
        companyAddress = in.readParcelable(Position.class.getClassLoader());
        orderBatchNumber = in.readParcelable(Position.class.getClassLoader());
        orderSerialNumber = in.readParcelable(Position.class.getClassLoader());
        orderDealTime = in.readParcelable(Position.class.getClassLoader());
        qr = in.readParcelable(Position.class.getClassLoader());
    }

    public static final Creator<Template> CREATOR = new Creator<Template>() {
        @Override
        public Template createFromParcel(Parcel in) {
            return new Template(in);
        }

        @Override
        public Template[] newArray(int size) {
            return new Template[size];
        }
    };

    @Override
    public String toString() {
        return type;
    }

    @Ignore
    public Template(int width, int height) {
        this.width = width;
        this.height = height;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null) return false;
        if (obj instanceof Template) {
            Template other = (Template) obj;
            return TextUtils.equals(this.type, other.type);
        }
        return false;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(type);
        dest.writeInt(width);
        dest.writeInt(height);
        dest.writeParcelable(title, flags);
        dest.writeParcelable(id1, flags);
        dest.writeParcelable(id2, flags);
        dest.writeParcelable(id3, flags);
        dest.writeParcelable(id4, flags);
        dest.writeParcelable(id5, flags);
        dest.writeParcelable(id6, flags);
        dest.writeParcelable(companyName, flags);
        dest.writeParcelable(companyContactNumber, flags);
        dest.writeParcelable(companyFaxNumber, flags);
        dest.writeParcelable(companyAddress, flags);
        dest.writeParcelable(orderBatchNumber, flags);
        dest.writeParcelable(orderSerialNumber, flags);
        dest.writeParcelable(orderDealTime, flags);
        dest.writeParcelable(qr, flags);
    }

    @Override
    public String getFirstLetter() {
        if (TextUtils.isEmpty(this.type)) {
            return "#";
        }
        String firstLetter = Cn2Spell.getPinYin(this.type).substring(0, 1).toUpperCase();
        if (!firstLetter.matches("[A-Z]")) {
            return "#";
        }
        return firstLetter;
    }

    @Override
    public String getItemName() {
        return this.type;
    }

    public static class Position implements Parcelable {

        public int x;
        public int y;
        public int size;

        public Position() {
        }

        @Ignore
        public Position(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Ignore
        public Position(int x, int y, int size) {
            this.x = x;
            this.y = y;
            this.size = size;
        }

        protected Position(Parcel in) {
            x = in.readInt();
            y = in.readInt();
            size = in.readInt();
        }

        public static final Creator<Position> CREATOR = new Creator<Position>() {
            @Override
            public Position createFromParcel(Parcel in) {
                return new Position(in);
            }

            @Override
            public Position[] newArray(int size) {
                return new Position[size];
            }
        };

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(x);
            dest.writeInt(y);
            dest.writeInt(size);
        }
    }
}